package models;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import play.db.DB;

/**
 * 
 * @author helton
 */
public class Evento {

	private int codigo;
	private Timestamp horarioInicial;
	private Timestamp horarioFinal;
	private String local;
	private String descricao;
	
	public Evento() {
		setCodigo(-1);
	}
	
	public Evento(int codigo, Timestamp horarioInicial, Timestamp horarioFinal, String local, String descricao) {
		this.codigo = codigo;
		this.horarioInicial = horarioInicial;
		this.horarioFinal = horarioFinal;
		this.local = local;
		this.descricao = descricao;
	}
	
	/**
     * Inserts the Evento object in the DB
     */
    public void save() throws SQLException {
    	// executeQuery para pegar o MAX de codigo
    	if(this.codigo == -1) {
    		ResultSet rs = DB.executeQuery("SELECT max(codigo) FROM evento");
    		rs.next();
    		
    		this.codigo = rs.getInt(1) + 1;
    	}
    	
    	SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm"); 
    	
    	DB.execute("INSERT INTO Evento(codigo, horario_inicial, horario_final, local, descricao) VALUES("
    			+ "'" + Integer.toString(this.codigo) + "',"
    			+ "'" + format.format(horarioInicial) + "',"
    			+ "'" + format.format(horarioFinal) + "',"
    			+ "'" + this.local + "',"
    			+ "'" + this.descricao + "'"
    			+ ")");
    }
    
    public static List<Evento> rangeDate(Date dataInicial, Date dataFinal) throws SQLException {
    	ResultSet rs = DB.executeQuery("SELECT * FROM evento WHERE "
    			+ "DATE(horario_inicial) >= '" + dataInicial.toString() + "' "
    				+ "AND DATE(horario_inicial) <= '" + dataFinal.toString() + "' ");
    	
    	List<Evento> lista = new ArrayList<Evento>();
    	
    	while(rs.next()) {
    		Evento evento = new Evento(rs.getInt(1), rs.getTimestamp(2), rs.getTimestamp(3),
    				rs.getString(4), rs.getString(5));
    		lista.add(evento);
    	}
    	
    	return lista;
    }
    
    public static List<Evento> all() throws SQLException {
    	ResultSet rs = DB.executeQuery("SELECT * FROM evento");
    	
    	List<Evento> lista = new ArrayList<Evento>();
    	
    	while(rs.next()) {
    		Evento evento = new Evento(rs.getInt(1), rs.getTimestamp(2), rs.getTimestamp(3),
    				rs.getString(4), rs.getString(5));
    		lista.add(evento);
    	}
    	
    	return lista;
    }
	
    public static List<Evento> allToPresenca() throws SQLException {
    	ResultSet rs = DB.executeQuery("SELECT * FROM evento WHERE codigo "
    			+ " NOT IN (SELECT codigo_evento FROM Presenca)");
    	
    	List<Evento> lista = new ArrayList<Evento>();
    	
    	while(rs.next()) {
    		Evento evento = new Evento(rs.getInt(1), rs.getTimestamp(2), rs.getTimestamp(3),
    				rs.getString(4), rs.getString(5));
    		lista.add(evento);
    	}
    	
    	return lista;
    }
    
	/**
	 * @param horarioInicial the horarioInicial to set
	 */
	public void setHorarioInicial(Timestamp horarioInicial) {
		this.horarioInicial = horarioInicial;
	}
	/**
	 * @return the horarioInicial
	 */
	public Timestamp getHorarioInicial() {
		return horarioInicial;
	}
	
	/**
	 * @return the horarioInicial in a string format
	 */
	public String getHorarioInicialString() {
		
		SimpleDateFormat format = new SimpleDateFormat("hh:mm dd/MM/yyyy"); 
		return format.format(horarioInicial);
	}
	
	/**
	 * @param horarioFinal the horarioFinal to set
	 */
	public void setHorarioFinal(Timestamp horarioFinal) {
		this.horarioFinal = horarioFinal;
	}
	
	/**
	 * @return the horarioFinal
	 */
	public Timestamp getHorarioFinal() {
		return horarioFinal;
	}
	
	/**
	 * @return the horarioInicial in a string format
	 */
	public String getHorarioFinalString() {
		
		SimpleDateFormat format = new SimpleDateFormat("hh:mm dd/MM/yyyy"); 
		return format.format(horarioFinal);
	}
	
	/**
	 * @param local the local to set
	 */
	public void setLocal(String local) {
		this.local = local;
	}
	/**
	 * @return the local
	 */
	public String getLocal() {
		return local;
	}
	
	/**
	 * @param descricao the descricao to set
	 */
	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}
	/**
	 * @return the descricao
	 */
	public String getDescricao() {
		return descricao;
	}
	/**
	 * @param codigo the codigo to set
	 */
	public void setCodigo(int codigo) {
		this.codigo = codigo;
	}
	/**
	 * @return the codigo
	 */
	public int getCodigo() {
		return codigo;
	}
	
	public static void delete(int codigoEvento) {
		DB.execute("DELETE FROM enquete WHERE codigo = '" + codigoEvento + "'");
	}
}
